// import Progress from "react-progress-2"
// import URLIP from './config'

// let token = window.localStorage.getItem('token')
import { UrlConfig } from '../services/fetch';

let getJson = (url) => {
    var fetchOptions = {
        method: "GET",
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json",
            'Authorization': window.localStorage.getItem('token'),
            // "token": window.localStorage.getItem('token')
        }
    };
    return new Promise((resolve, reject) => {
        // Progress.show();
        fetch(url, fetchOptions).then(function (response) {
            return response.json();
        }).then(function (json) {
            resolve(json);
            // Progress.hide();
        })
    });
}

let postJson = (url, data) => {
    var fetchOptions = {
        method: "POST",
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json",
            'Authorization': window.localStorage.getItem('token'),
        },
        
        body: JSON.stringify(data)
    };
    return new Promise((resolve, reject) => {
        // Progress.show();
        fetch(url, fetchOptions).then((response) => response.json()).then((data) => {
            resolve(data);
            // Progress.hide();
        })
    })
}
let postJson2 = (url, data) => {
    var fetchOptions = {
        method: "POST",
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        body: "signstr="+encodeURIComponent(data.signstr)
    };
    return new Promise((resolve, reject) => {   
        fetch(url, fetchOptions).then((response) => response.json()).then((data) => {
            resolve(data);   
        })
    })
}
let Http = (url, method, data) => {
    if(data.hasOwnProperty("signstr")){
        return postJson2(url, data)
    }else{
    let tempUrl = UrlConfig
    url = tempUrl + url
    if (method === 'get') {
        let urls = '?'
        for (const key in data) {
            if (data.hasOwnProperty(key)) {
                urls = `${urls}${key}=${data[key]}&`
            }
        }
        url = url + urls
        return getJson(url)
    }
    if (method === 'post') {
        return postJson(url, data)
    }
  }
}
//样式转文字
export function styleToText(text) {
    let info = '默认';
    switch (text) {
        case 'news_list':
            info = '左图样式';
            break;
        case 'news_banner':
            info = '直角banner';
            break;
        case 'news_express':
            info = '快讯';
            break;
        case 'news_service':
            info = '服务';
            break;
        case 'news_banner2':
            info = '圆角banner';
            break;
        case 'news_right_img':
            info = '右图样式';
            break;
        case 'news_big_img':
            info = '大图样式';
            break;
        case 'news_card':
            info = '卡片样式';
            break;
        case 'jx_banner':
            info = '矩形banner';
            break;
    }

    return info;
}

export default Http;
